﻿using System;
using System.Web.Services;
using Besture.Common.Definition;
using Besture.Common.Helper;
using Besture.Model;
using Besture.UI.ClassHelper;
using DevExpress.Web.ASPxGridView;
using DevExpress.Web.Data;
using Besture.Common;
using Besture.Bll;

namespace Besture.UI.Admin.Content
{
    public partial class PartContentList : BasePage
    {
        //栏目编号
        public string PartID = StringHelper.Query("partid");

        #region 页面加载

        /// <summary>
        /// 初始化页面
        /// </summary>
        protected override void InitialLoad()
        {
            //初始化页面
            InitPage();
            BindData();
            AspxGridViewHelper.InitAspxGridView(grid);
        }

        /// <summary>
        /// 回发
        /// </summary>
        protected override void PostBackLoad()
        {
            BindData();
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        [WebMethod]
        public static string DeleteData(int id)
        {
            PartContentDac dac = new PartContentDac();
            if (dac.Delete(id) > 0)
            {
                return id.ToString();
            }
            return "0";
        }

        /// <summary>
        /// 初始化页面
        /// </summary>
        public void InitPage()
        {
            //显示数据源
            //dataSourceDisplay.ConnectionString = AppSettings.ConnectionString;
            //dataSourceDisplay.SelectCommand = "SELECT NAME,[Value] FROM TBDictionary WHERE FlagType = 3 UNION SELECT '全部' AS NAME ,'' AS Value";
            //dataSourceDisplay.DataBind();
            ////网站置顶数据源
            //dataSourceWebStick.ConnectionString = AppSettings.ConnectionString;
            //dataSourceWebStick.SelectCommand = "SELECT NAME,[Value] FROM TBDictionary WHERE FlagType = 4 UNION SELECT '全部' AS NAME ,'' AS Value";
            //dataSourceWebStick.DataBind();
            ////栏目置顶数据源
            //dataSourceClassStick.ConnectionString = AppSettings.ConnectionString;
            //dataSourceClassStick.SelectCommand = "SELECT NAME,[Value] FROM TBDictionary WHERE FlagType = 5 UNION SELECT '全部' AS NAME ,'' AS Value";
            //dataSourceClassStick.DataBind();
        }

        /// <summary>
        /// 绑定数据
        /// </summary>
        public void BindData()
        {
            if (string.IsNullOrEmpty(PartID))
            {
                PartID = "0";
            }

            string sql =
                string.Format(
                    @"SELECT 
	                    tc.*,t.Name AS PartName,t.Name AS PartName,CASE tc.IsDisplay WHEN 1 THEN '显示' ELSE '隐藏' END TCDisplay
                        ,CASE tc.WebStick WHEN 1 THEN '是' ELSE '否' END TcWebStick 
                    FROM 
	                    TBPartContent tc
                    LEFT JOIN 
	                    TBPart t 
                    ON 
	                    tc.PartID = t.ID 
                    WHERE 
	                    ( tc.partid IN (SELECT ID FROM PartTree({0}))
                    OR 
	                    tc.partid = {0} )
                    ORDER BY
                        tc.id DESC", PartID);
            grid.DataSource = QueryDac.GetTable(sql);
            grid.DataBind();
        }

        /// <summary>
        /// AspxGridView回发事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void grid_CustomCallback(object sender, ASPxGridViewCustomCallbackEventArgs e)
        {
            if (e.Parameters == "Refresh")
                grid.DataBind();
        }

        #endregion

        #region 删除数据

        /// <summary>
        /// 删除单条数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void grid_RowDeleting(object sender, ASPxDataDeletingEventArgs e)
        {
            e.Cancel = true;
            int id = Convert.ToInt32(e.Keys[0]);
            PartContentDac partContentDac = new PartContentDac();
            partContentDac.Delete(id);
            BindData();
        }

        /// <summary>
        /// 删除选中数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void lbDel_Click(object sender, EventArgs e)
        {
            string ids = AspxGridViewHelper.GetCheckID(grid);
            if (string.IsNullOrEmpty(ids))
            {
                return;
            }
            PartContentDac partContentDac = new PartContentDac();
            partContentDac.DeleteList(ids);
            FunHelper.Alert("删除成功！", Request.Url.ToString());
        }


        protected void btnProButton_Click(object sender, EventArgs e)
        {
            string ids = AspxGridViewHelper.GetCheckID(grid);
           if (string.IsNullOrEmpty(ids))
           {
               return;
           }
            PartContentDac partContentDac = new PartContentDac();
            string proType = txtProType.Text;
            if(string.IsNullOrEmpty(proType))
            {
                PageAlert("发生异常");
                return;
            }
            switch (proType)
            {
                //设置显示
                case "1":
                    partContentDac.SetDisplay(true, ids);
                    break;
                //设置隐藏
                case "2":
                    partContentDac.SetDisplay(false, ids);
                    break;
                //设置网站置顶
                case "3":
                    partContentDac.SetWebStick(true, ids);
                    break;
                //取消网站置顶
                case "4":
                    partContentDac.SetWebStick(false, ids);
                    break;
                //设置栏目置顶
                case "5":
                    partContentDac.SetPartStick(true, ids);
                    break;
                //取消栏目置顶
                case "6":
                    partContentDac.SetPartStick(false, ids);
                    break;
            }
            FunHelper.Alert("设置成功！", Request.Url.ToString());
        }

        #endregion

        #region 转向添加页面

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="eventArgs"></param>
        protected void lbAdd_Click(object obj, EventArgs eventArgs)
        {
            RedirectUrl("PartContentSet.aspx?partid=" + PartID);
        }

        #endregion

        /// <summary>
        /// 导出EXCEL
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnExport_Click(object sender, EventArgs e)
        {
            string ids = AspxGridViewHelper.GetCheckID(grid);
            if (!string.IsNullOrEmpty(ids))
            {
                gridExport.WriteXlsToResponse();
            }
            else
            {
                ClientScript.RegisterStartupScript(this.GetType(), "", "alert('请选择要导出的数据！');", true);
            }
        }



    }
}